// packageProductInfo/page/payInfo/payInfo.js
import user from '../../../utils/user'
import {
    getAccountExpireNotify,
    getAccountProductInfo,
    getadphone,
    getInsuranceRecharge,
    getIongInsuranceRechargeDetail,
    getEleSignRechargeRecord,
} from '../../../api/app'
import dayjs from 'dayjs'
Page({
    /**
     * 页面的初始数据
     */
    data: {
        isOpenWesure: '',
        isOpenPeriodInsur: '',
        isEleSign: '',
        tyxSign: '',
        phonelist: [],
        isShowTips: false,
        companyName: '',
        auditStatus: false,
        isOpenPay: false,
        expirationTime: {}, // 到期时间
        accountProductInfo: {}, // 卡包信息
        currentTab: 1,
        wechaitunopen: true,
        alipayunopen: true,
        isup: false,
        list: [],
        pageSize: 10,
        pageNum: 1,
        hasNext: true,
    },
    scrollEnd() {
        if (!this.data.hasNext) {
            return
        }
        this.setData({
            page: this.data.pageNum += 1
        })
        if (this.data.currentTab === 1) {
            this.getIongInsuranceRechargeDetail()
        } else if (this.data.currentTab === 2) {
            this.onGetInsuranceRecharge()
        } else if (this.data.currentTab === 3) {
            this.onGetInsuranceRechargec()
        } else if (this.data.currentTab === 4) {
            this.getEleSignRechargeRecord()
        }
    },
    onToSubscribe() {
        wx.navigateTo({
            url: '/pages/subscribe/subscribe',
        })
    },
    onGetInsuranceRechargec() {
        if (!this.data.tyxSign) {
            return
        }
        // 日日保接口 和 团意保使用同一个接口 通过 insureCompanyType 字段来区分
        getInsuranceRecharge({
            insureCompanyType: 'insure_type_tyrb',
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize
        }).then(res => {
            if (!res.hasNextPage) {
                this.setData({
                    hasNext: false
                })
            }
            const arr = []
            arr.push(...this.data.list)
            arr.push(...res.list.map(item => {
                item.payMoney = item.rechargeAmount
                return item
            }))
            this.setData({
                list: arr
            })
        })
    },
    onGetInsuranceRecharge() {
        if (!this.data.isOpenWesure) {
            return
        }
        // 日日保接口
        getInsuranceRecharge({
            insureCompanyType: user.insureCompanyType,
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize
        }).then(res => {
            if (!res.hasNextPage) {
                this.setData({
                    hasNext: false
                })
            }
            const arr = []
            arr.push(...this.data.list)
            arr.push(...res.list.map(item => {
                item.payMoney = item.rechargeAmount
                return item
            }))
            this.setData({
                list: arr
            })
        })
    },
    // 获取长期工充值列表
    async getIongInsuranceRechargeDetail() {
        if (!this.data.isOpenPeriodInsur) {
            return
        }
        const result = await getIongInsuranceRechargeDetail({
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize
        })
        if (!result.hasNextPage) {
            this.setData({
                hasNext: false
            })
        }
        const arr = []
        arr.push(...this.data.list)
        arr.push(...result.list.map((item) => {
            item.payMoney = item.rechargeAmount
            return item
        }))
        this.setData({
            list: arr
        })
    },
    async getEleSignRechargeRecord() {
        // 电子签
        const result = await getEleSignRechargeRecord({
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize
        })
        if (!result.hasNextPage) {
            this.setData({
                hasNext: false
            })
        }
        const arr = []
        arr.push(...this.data.list)
        arr.push(...result.list)
        this.setData({
            list: arr
        })
    },
    showPhone() {
        let arr = []
        if (this.data.phonelist.length !== 0) {
            this.data.phonelist.forEach((ele) => {
                arr.push(ele.phone + '-' + ele.name)
            })
        } else {
            arr.push('15950927000')
        }

        wx.showActionSheet({
            itemList: arr,
            success: (res) => {
                if (this.data.phonelist.length !== 0) {
                    this.callPhone(this.data.phonelist[res.tapIndex].phone + '')
                } else {
                    this.callPhone('15950927000')
                }
            },
            fail(res) {
                console.log(res.errMsg)
            }
        })
    },
    onOpenpopup() {
        this.setData({
            isup: true
        })
    },
    onClosePopup() {
        this.setData({
            isup: false
        })
    },
    onMyRecharge(e) {
        console.log(e, user.insureCompanyType)
        if (e.currentTarget.dataset.type === 'app') {
            wx.navigateToMiniProgram({
                appId: e.currentTarget.dataset.path,
            })
        } else {
            if (user.insureCompanyType === 'insure001_wesure') {
                wx.showToast({
                    title: '功能维护中',
                    icon: 'none'
                })
                return
            } else {
                let str = e.currentTarget.dataset.type ? '?type=' + e.currentTarget.dataset.type : ''
                wx.navigateTo({
                    url: e.currentTarget.dataset.path + str
                })
            }

        }
    },
    // 提示信息
    onTips() {
        console.log('点击啦')
        this.setData({
            isShowTips: !this.data.isShowTips
        })
    },
    onChangeTab(e) {
        let i = e.currentTarget.dataset.i
        this.setData({
            list: [],
            pageNum: 1,
            pageSize: 10,
            hasNext: true,
            currentTab: i
        })
        if (i === 1) {
            this.getIongInsuranceRechargeDetail()
        } else if (i === 2) {
            this.onGetInsuranceRecharge()
        } else if (i === 3) {
            this.onGetInsuranceRechargec()
        } else if (i === 4) {
            this.getEleSignRechargeRecord()
        }
    },
    onCallPhone() {
        console.log('打电话')
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        user.waitLogin().then(async () => {
            this.setData({
                companyName: user.companyName,
                auditStatus: user.auditStatus,
                isOpenPay: !!user.isOpenPaySalary,
                isOpenWesure: !!user.isOpenWesure,
                isOpenPeriodInsur: !!user.isOpenPeriodInsur,
                currentTab: Number(options.currrent)
            })
            await getadphone().then((res) => {
                if (res.length === 0) {
                    return
                } else {
                    this.data.phonelist = res.map((ele) => {
                        return {
                            name: ele.salesName,
                            phone: ele.phone
                        }
                    })
                    this.setData({
                        phonelist: this.data.phonelist
                    })
                }
            })
            await this.getAccountExpireNotify()
            await this.getAccountProductInfo()
            if (Number(options.currrent) === 1) {
                this.getIongInsuranceRechargeDetail()
            } else if (Number(options.currrent) === 2) {
                this.onGetInsuranceRecharge()
            } else if (Number(options.currrent) === 3) {
                this.onGetInsuranceRechargec()
            } else if (Number(options.currrent) === 4) {
                this.getEleSignRechargeRecord()
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},
    async getAccountProductInfo() {
        const result = await getAccountProductInfo()
        this.setData({
            accountProductInfo: result,
            tyxSign: !!result.tyxSign.isOpen,
            isEleSign: !!Number(result.eleSign.isOpen ? 1 : 0),
        })
    },
    // 账户到期提示接口
    async getAccountExpireNotify() {
        const result = await getAccountExpireNotify()
        result.accountExpireTime = result.accountExpireTime ? dayjs(result.accountExpireTime).format('YYYY/MM/DD').replace(/\//g, '-') : null
        this.setData({
            expirationTime: result
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})